字符串的格式化
视频锁定
{$ currentTime | date:'mm:ss' $}
{$ timeLeft | date:'mm:ss' $}
我们知道输出格式化数字可以使用printf()
和format()
方法。String类使用静态方法format()
返回一个String对象而不是PrintStream对象。
String类的静态方法format()
能用来创建可复用的格式化字符串,而不仅仅是用于一次打印输出。如下所示:
System.out.printf("The value of the float variable is " +
"%f, while the value of the integer " +
"variable is %d, and the string " +
"is %s", floatVar, intVar, stringVar);
你也可以这样写
String fs;
fs = String.format("The value of the float variable is " +
"%f, while the value of the integer " +
"variable is %d, and the string " +
"is %s", floatVar, intVar, stringVar);
System.out.println(fs);
在线练习
{$ activeFileHint $}